
#include "../ui.h"

#define UI_FADE_IN_DELAY        (0)
#define HOMEPAGE_PAGE_NUM       (2)

typedef struct
{
    const char *name;
    const lv_img_dsc_t *icon;
    fun_app_open_t open;
    fun_app_close_t close;
    uint8_t page_index;
}app_icons_data_t;

LV_IMG_DECLARE(img_lv_icon_solder);
LV_IMG_DECLARE(img_lv_icon_oscilloscope);
LV_IMG_DECLARE(img_lv_icon_multimeter);
LV_IMG_DECLARE(img_lv_icon_signal);
LV_IMG_DECLARE(img_lv_icon_setting);

static app_icons_data_t app_data[] =
{
    {"焊台",          &img_lv_icon_solder,        lv_solder_open, lv_solder_close, 0,},
    {"示波器",    &img_lv_icon_oscilloscope,  lv_oscilloscope_open,lv_oscilloscope_close,0,},
    {"万用表",      &img_lv_icon_multimeter,    lv_mutimeter_open, lv_mutimeter_close, 0,},
    {"信号发生器",          &img_lv_icon_signal,        lv_signal_open,NULL, 0,},
    {"电源",         &img_lv_icon_setting,       NULL,NULL, 1,},
};

static ui_homepage_t *ui = &ui_homepage;
static lv_obj_t *ui_tab[HOMEPAGE_PAGE_NUM];

static void homepage_init(void);

static void homepage_quit(lv_event_t *e);
static void homepage_icons_click(lv_event_t *e);

lv_obj_t *lv_homepage_load(void)
{
    //lv_obj_clean(ui->screen); //清屏

    ui->screen = lv_screen_create(ui_main_screen.desktop, LV_HOR_RES, LV_VER_RES);

/*
    LV_IMG_DECLARE(ui_img_bg_png);
    ui->bg = lv_img_create(ui->screen);
    lv_img_set_src(ui->bg, &ui_img_bg_png);
    lv_obj_align(ui->bg, LV_ALIGN_CENTER,0,0);
*/
    homepage_init();

    //lv_obj_add_event_cb(ui->screen, homepage_quit, LV_EVENT_SCREEN_UNLOADED, NULL);

  return ui->screen;
}

static void homepage_add_tab_rounds(lv_obj_t *tab, uint8_t index)
{
    uint8_t i;
    lv_obj_t *round;
    for(i=0;i!=HOMEPAGE_PAGE_NUM;i++)
    {
        round = lv_obj_create(tab);
        if(index==i)
            lv_obj_set_style_bg_opa(round, LV_OPA_80, LV_PART_MAIN|LV_STATE_DEFAULT); // 设置背景透明度
        else
            lv_obj_set_style_bg_opa(round, LV_OPA_30, LV_PART_MAIN|LV_STATE_DEFAULT);
        lv_obj_set_style_radius(round, 100, LV_PART_MAIN|LV_STATE_DEFAULT);                // 设置圆角
        lv_obj_set_style_bg_color(round, lv_color_white(), LV_PART_MAIN|LV_STATE_DEFAULT);   // 设置颜色
        lv_obj_set_size(round, 8, 8);
        lv_obj_align(round, LV_ALIGN_BOTTOM_LEFT, 200+i*40, 0);
    }

}

static void homepage_quit(lv_event_t *e)
{
    lv_event_code_t event_code = lv_event_get_code(e);
    if(event_code==LV_EVENT_SCREEN_UNLOADED) {
          ;//lv_obj_del(ui->screen);
    }

}

static void homepage_icons_click(lv_event_t *e)
{
    lv_obj_t *screen;
    ui->page_select = lv_tabview_get_tab_act(ui->tabview);
    app_icons_data_t * app = (app_icons_data_t *)(e->user_data);
    if(app->open!=NULL) {
        lv_obj_del(ui->screen);
        ui_main_screen.current_desktop = app->open();
        ui_main_screen.app_close = app->close;
        //lv_scr_load_anim(screen, LV_SCR_LOAD_ANIM_OUT_LEFT, 100, 0, false);
    }
}

static void homepage_add_icons(void)
{
    uint8_t i, col_cnt[HOMEPAGE_PAGE_NUM], row_cnt[HOMEPAGE_PAGE_NUM];
    lv_obj_t *icon_name;
    lv_coord_t col_space = 40;
    lv_coord_t row_space = 40;
    uint16_t icon_size = 60;
    memset(col_cnt, 0 , HOMEPAGE_PAGE_NUM);
    memset(row_cnt, 0 , HOMEPAGE_PAGE_NUM);
    for(i=0;i!=UI_HOMEPAGE_APP_NUM;i++) {
        ui->app_icons[i] = lv_img_create(ui_tab[app_data[i].page_index]);
        lv_img_set_src(ui->app_icons[i],app_data[i].icon);
        lv_obj_add_flag(ui->app_icons[i], LV_OBJ_FLAG_CLICKABLE);
        lv_obj_align_to(ui->app_icons[i], ui_tab[app_data[i].page_index], LV_ALIGN_TOP_LEFT,
                        col_space+col_cnt[app_data[i].page_index]*(col_space+icon_size),
                        480-(20+(row_cnt[app_data[i].page_index]+1)*(row_space+icon_size+20))-200);

        icon_name = lv_label_create(ui_tab[app_data[i].page_index]);
        lv_label_set_text_static(icon_name, app_data[i].name);
        lv_obj_set_style_text_font(icon_name, &ui_font_FontChineseSmall, LV_PART_MAIN| LV_STATE_DEFAULT);
        lv_obj_align_to(icon_name,ui->app_icons[i], LV_ALIGN_OUT_BOTTOM_MID, 0, 4);
        lv_obj_add_event_cb(ui->app_icons[i], homepage_icons_click, LV_EVENT_CLICKED, &app_data[i]);

        col_cnt[app_data[i].page_index]++;
        if(col_cnt[app_data[i].page_index]==4) { //换行显示
            col_cnt[app_data[i].page_index] = 0;
            row_cnt[app_data[i].page_index]++;
        }
    }
}

static void homepage_init(void)
{
    uint16_t i;
    ui->tabview = lv_tabview_create(ui->screen, LV_DIR_TOP, 0);
    lv_obj_set_size(ui->tabview, LV_HOR_RES, LV_VER_RES);
    lv_obj_align(ui->tabview, LV_ALIGN_TOP_MID, 0, -20);

    lv_obj_set_style_bg_opa(ui->tabview, LV_OPA_0, LV_PART_MAIN|LV_STATE_DEFAULT);
    for(i=0;i!=HOMEPAGE_PAGE_NUM;i++) {
        ui_tab[i] = lv_tabview_add_tab(ui->tabview, "1");
        homepage_add_tab_rounds(ui_tab[i], i);
    }
    homepage_add_icons();
    lv_tabview_set_act(ui->tabview, ui->page_select, false);
}
